import ast

def string_to_list(input_string):
    """
    将字符串格式的列表转换为列表格式的列表
    
    参数:
    input_string (str): 字符串格式的列表，例如 '[["文生图", "描述"], ["图生视频", "描述"]]'
    
    返回:
    list: 转换后的列表
    """
    try:
        # 使用ast.literal_eval安全地评估字符串
        result = ast.literal_eval(input_string)
        if isinstance(result, list):
            return result
        else:
            raise ValueError("输入字符串不是有效的列表格式")
    except (SyntaxError, ValueError) as e:
        print(f"转换失败: {e}")
        return None
def organize_tasks(task_list):
    # 创建一个字典来存储依赖关系
    dependency_dict = {}
    
    # 首先将所有任务按照它们的依赖索引分组
    for index, task in enumerate(task_list):
        dependency_index = task[2]
        if dependency_index not in dependency_dict:
            dependency_dict[dependency_index] = []
        dependency_dict[dependency_index].append((index, task))
    
    # 初始化结果列表
    result = []
    
    # 处理没有依赖的任务（dependency_index = -1）
    independent_tasks = dependency_dict.get(-1, [])
    for _, task in independent_tasks:
        # 对于每个独立任务，检查是否有其他任务依赖它
        dependent_tasks = dependency_dict.get(task_list.index([task[0], task[1], -1]), [])
        if dependent_tasks:
            for dep_index, dep_task in dependent_tasks:
                # 创建依赖链
                chain = [[task[0], task[1]], [dep_task[0], dep_task[1]]]
                result.append(chain)
        else:
            # 如果没有依赖任务，单独作为一个链
            result.append([[task[0], task[1]]])
    
    return result


if __name__ == "__main__":
        # 示例输入
    input_list = [
        ["文生图", "A cute orange tabby cat sitting on a white background, highly detailed fur, bright eyes, photorealistic style, 8K resolution", -1],
        ["文生图", "A small black dog riding a bicycle on a sunny road, cartoonish style, playful expression, dynamic motion blur in background", -1],
        ["图生视频", "Make the cat fly freely in the sky with fluffy clouds, slow wing-flapping motion, cinematic drone camera angle, dreamy lighting", 0],
        ["图生视频", "Animate the dog pedaling the bicycle smoothly, add passing trees and road textures, loopable 5-second clip, 24fps", 1],
        ["图生3D", "Convert this cat photo into a stylized 3D model with rigged skeleton, soft fur texture, and expressive face, OBJ format", 0]
    ]

    # 调用函数并打印结果
    output = organize_tasks(input_list)
    for chain in output:
        print(chain)